﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace NNLInterface
{
    public partial class frmBaseBasicEntry : NNLInterface.frmBaseBasic
    {
        #region 变量定义

        public frmBaseBasicEntry()
        {
            InitializeComponent();
            this.SetToolVisible(common.ToolVisible.BasicEntry);
            this.ListView.BeforeLeaveRow += new DevExpress.XtraGrid.Views.Base.RowAllowEventHandler(ListView_BeforeLeaveRow);
        }

        void ListView_BeforeLeaveRow(object sender, DevExpress.XtraGrid.Views.Base.RowAllowEventArgs e)
        {
            if (this.Mode!=common.DataModifyMode.view)
            {
                e.Allow=false;
            }
        }


        private DevExpress.XtraLayout.LayoutControl _layOUT;
        public DevExpress.XtraLayout.LayoutControl layOUT
        {
            get
            {
                return _layOUT;
            }
            set
            {
                if (_layOUT == value)
                    return;
                _layOUT = value;
            }
        }

        private bool _executeFlag = false;
        public bool executeFlag
        {
            get
            {
                return _executeFlag;
            }
            set
            {
                if (_executeFlag == value)
                    return;
                _executeFlag = value;
            }
        }

        private bool _CustomReport;
        public bool CustomReport
        {
            get
            {
                return _CustomReport;
            }
            set
            {
                if (_CustomReport == value)
                    return;
                _CustomReport = value;
            }
        }

        #region 属性定义

        private string _v_Open_View = "";
        public string v_Open_View
        {
            get
            {
                return _v_Open_View;
            }
            set
            {
                if (_v_Open_View == value)
                    return;
                _v_Open_View = value;
            }
        }
        private string _v_Field_Key_Name = "";
        public string v_Field_Key_Name
        {
            get
            {
                return _v_Field_Key_Name;
            }
            set
            {
                if (_v_Field_Key_Name == value)
                    return;
                _v_Field_Key_Name = value;
            }
        }

        private common.DataModifyMode _mode;
        public common.DataModifyMode Mode
        {
            get
            {
                return _mode;
            }
            set
            {
                if (_mode == value)
                    return;
                _mode = value;
            }
        }

        private string _strUpdateSql;
        public string strUpdateSql
        {
            get
            {
                return _strUpdateSql;
            }
            set
            {
                if (_strUpdateSql == value)
                    return;
                _strUpdateSql = value;
            }
        }

        private string _strInsertSql;
        public string strInsertSql
        {
            get
            {
                return _strInsertSql;
            }
            set
            {
                if (_strInsertSql == value)
                    return;
                _strInsertSql = value;
            }
        }

        private string _strDeleteSql;
        public string strDeleteSql
        {
            get
            {
                return _strDeleteSql;
            }
            set
            {
                if (_strDeleteSql == value)
                    return;
                _strDeleteSql = value;
            }
        }
        #endregion


        public delegate void  EditButtonClickHandler(common.DataModifyMode dataMode);
        public event EditButtonClickHandler EditButtononClick;

     
        #endregion

        #region 初始化


        
            /// <summary>
            /// 窗体Load事件时引用此方法
            /// </summary>
            /// <param name="v_Open_View">窗体表格视图</param>
            /// <param name="v_Field_Key_Name">主key</param>
            public void iniFormData(string v_Open_view, string v_Field_Key_name)
            {
                try
                {
                    this.CustomReport=false;
                    this.ListView.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(ListView_CustomDrawRowIndicator);
                    this.ListView.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(ListView_FocusedRowChanged);
                    this.EditButtononClick += new EditButtonClickHandler(frmBaseBasicEntry_EditButtononClick);

                    //this.v_Open_View = common.GetLanguageWord(this.Name, "v_open_view");
                    //this.v_Field_Key_Name = common.GetLanguageWord(this.Name, "v_Field_Key_Name");

                    this.v_Open_View = v_Open_view;
                    this.v_Field_Key_Name = v_Field_Key_name;

                    string strSql = "Select   * From " + this.v_Open_View + "  Order by " + this.v_Field_Key_Name;
                    this.SetToolVisible(common.ToolVisible.BasicEntry);
                    common.subIniGrid(this.ListView, common.enumGridStyle.ViewStyle);
                    common.SubShowList(strSql, ListGrid, ListView, false);
                    subSetGridFormat();
                    common.ReStoreLayOut(ListView);
                    subRestoreFormLayOut();
                    common.GetResourceString(this, this.Name);
                    common.subSetCmdControl(common.DataModifyMode.view, (frmBaseToolXC)this, this.ListView.RowCount);
                    common.ClsFormControl(this);
                    common.SetFormReadOnly(this, true);
                    subSetValidCondition();
                    subSetDataSource();
                    ListGrid.Focus();
                    if (ListView.RowCount >= 1)
                    {
                        this.ListView.MoveLast();
                        //DataRow dt = ((DataView)ListView.DataSource).ToTable().Rows[0];
                        DataRow dt = ListView.GetFocusedDataRow();
                        subSetData(dt);
                    }
                    //common.SetFormPower(this);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                finally
                {

                }
            }

            protected virtual  void subSetGridFormat()
            {
               
            }
        
        #endregion

        #region 工具栏事件

        private void frmBaseBasicEntry_ToolButtonClick(object sender, string e)
        {
            try
            {
                switch (e)
                {
                    case "new":
                        SubAddNew();
                        break;
                    case "edit":
                        SubEdit();
                        break;
                    case "delete":
                        this.EditButtononClick(this.Mode);
                        SubDelete();
                        break;
                    case "save":
                        this.EditButtononClick(this.Mode);
                        SubSaveData( );
                        break;
                    case "exit":
                        subSaveFormLayOut();
                        this.Dispose();
                        break;
                    case "cancel":
                        SubCancel();
                        break;
                    case "print":
                        if (!this.CustomReport)
                            common.PrintView(this.ListGrid, common.GetLanguageWord(this.Name, this.Text));
                        else
                            subShowReport();
                        break;
                    case "excel":
                        common.ExportExcel(this.ListView);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected virtual void subShowReport()
        {
            
        }

        private void SubDelete()
        {
            if (!CheckDeleteData()) return;
            if (DevExpress.XtraEditors.XtraMessageBox.Show("确定要删除此笔数据吗(Y/N)?", common.sysParameter.MessageInf, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
            string strSql = " set xact_abort on begin tran " + this.strDeleteSql  + " commit tran ";
            bool status = accessdb.RsChange(strSql);
            if (!status)
            {
                //"delete user data to interdatabase error!"
                DevExpress.XtraEditors.XtraMessageBox.Show("删除数据出错", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.strInsertSql = "";
                this.strUpdateSql = "";
                this.strDeleteSql = "";
                return;
            }
            this.Mode  = common.DataModifyMode.view;
         
            strSql = "Select   * From " + this.v_Open_View + "  Order by " + this.v_Field_Key_Name;
            common.SubShowList(strSql, ListGrid, ListView, false);
            common.subSetCmdControl(common.DataModifyMode.view, (frmBaseToolXC)this, this.ListView.RowCount);
            common.ClsFormControl(this);
            common.SetFormReadOnly(this, true);
            this.strInsertSql = "";
            this.strUpdateSql = "";
            this.strDeleteSql = "";
            ListGrid.Focus();
            if (ListView.RowCount >= 1)
            {
                DataRow dt = ListView.GetFocusedDataRow();
                subSetData(dt);
            }
        
            //common.GetResourceString(this,this.Name );

        }



        private void SubEdit()
        {
            if (!CheckEditData())
            {
                this.strInsertSql = "";
                this.strUpdateSql = "";
                this.strDeleteSql = "";
                return;
            }
            this.Mode = common.DataModifyMode.edit;
            common.subSetCmdControl(common.DataModifyMode.edit, (frmBaseToolXC)this, this.ListView.RowCount);
            //common.ClsFormControl(this);
            common.SetFormReadOnly(this, false);
            subSetContolRead();
            subSetDataSource();
        }

        protected virtual bool CheckEditData()
        {
            return true;
        }

        private void SubCancel()
        {
            if (DevExpress.XtraEditors.XtraMessageBox.Show("确定要取消吗(Y/N)?", common.sysParameter.MessageInf, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;

            IList<Control> controlList = this.validData.GetInvalidControls();
            System.Windows.Forms.Control[] ctl = new Control[controlList.Count];

            if (controlList.Count >= 1)
            {
                for (int i = 0; i < controlList.Count; i++)
                {
                    ctl[i] = controlList[i];
                }
            }

            for (int i = 0; i < ctl.Length; i++)
            {
                this.validData.RemoveControlError(ctl[i]);
            }
           
          
            string strSql = "Select   * From " + this.v_Open_View + "  Order by " + this.v_Field_Key_Name;
            common.SubShowList(strSql, this.ListGrid, this.ListView, false);
            common.subSetCmdControl(common.DataModifyMode.view, (frmBaseToolXC)this, this.ListView.RowCount);
            common.ClsFormControl(this);
            common.SetFormReadOnly(this, true);
            subSetDataSource();
            this.ListGrid.Focus();
            if (this.ListView.RowCount >= 1)
            {
                DataRow dt = ListView.GetFocusedDataRow();
                subSetData(dt);
            }
            common.subIniGrid(this.ListView, common.enumGridStyle.ViewStyle);
            this.strInsertSql = "";
            this.strUpdateSql = "";
            this.strDeleteSql = "";
            this.Mode = common.DataModifyMode.view;
            //common.GetResourceString(this, this.Name);
        }

        private void SubAddNew()
        {
            this.Mode = common.DataModifyMode.add;
            //如果有数据，则提示是否复制当前据
            //if (this.ListView.RowCount>=1)
            //    if (DevExpress.XtraEditors.XtraMessageBox.Show("复制当前数据吗(Y/N)?", common.sysParameter.MessageInf, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            common.ClsFormControl(this);
            common.subSetCmdControl(common.DataModifyMode.add, (frmBaseToolXC)this, this.ListView.RowCount);
            common.SetFormReadOnly(this, false);
            subSetDataSource();
            //subSetGridFormat();
            //ListGrid.Enabled = false;
        }

        private void SubSaveData( )
        {
            
            //if (!this.validData.Validate()) return;
            //if (!CheckSaveData()) return;


            if (!this.validData.Validate())
            {
                this.strInsertSql = "";
                this.strUpdateSql = "";
                this.strDeleteSql = "";
                return;
            }

            if (!CheckSaveData())
            {
                this.strInsertSql = "";
                this.strUpdateSql = "";
                this.strDeleteSql = "";
                return;
            }

            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            bool status;
            string strSql;
            if(!this.executeFlag)
            {
                strSql = " set xact_abort on begin tran " + (this.Mode == common.DataModifyMode.add ?
                        this.strInsertSql : this.strUpdateSql) + " commit tran ";
                status = accessdb.RsChange(strSql);
            }
            else
            {
                status = ExecutePro();
            }

            if (!status)
            {
                //"save user data to interdatabase error!"
                Cursor.Current = currentCursor;
                this.strInsertSql = "";
                this.strUpdateSql = "";
                this.strDeleteSql = "";
                //DevExpress.XtraEditors.XtraMessageBox.Show("保存数据出错！", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
           
            strSql = "Select   * From " + this.v_Open_View + "  Order by " + this.v_Field_Key_Name;
            common.SubShowList(strSql, ListGrid, ListView, false);
            this.ListView.MoveLast();
            common.subSetCmdControl(common.DataModifyMode.view, (frmBaseToolXC)this, this.ListView.RowCount);
            common.ClsFormControl(this);
            common.SetFormReadOnly(this, true);
            DataRow dt = ListView.GetFocusedDataRow();
            subSetData(dt);
            //common.GetResourceString(this, this.Name);
            this.ListGrid.Focus();
            this.Mode = common.DataModifyMode.view;
            this.strInsertSql = "";
            this.strUpdateSql = "";
            this.strDeleteSql = "";
            Cursor.Current = currentCursor;

        }

        protected virtual bool ExecutePro()
        {
            return true;
        }



        protected void frmBaseBasicEntry_EditButtononClick(common.DataModifyMode dataMode)
        {
           
        }


        #endregion

        #region 数据表格事件

        protected void ListView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (this.Mode == common.DataModifyMode.view && this.ListView.RowCount >= 1)
            {
                DataRow dr = ListView.GetDataRow(e.FocusedRowHandle);
                if (dr != null)
                    subSetData(dr);
            }
        }

        private void ListView_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "组" + e.RowHandle.ToString();
                }
            }
        }
        #endregion

        #region 子窗体要重写的方法

        protected virtual void subSetData(DataRow dr)
        {
            
        }

        protected virtual bool CheckSaveData()
        {
            return true;
        }

        protected virtual bool CheckDeleteData()
        {
            return true;
        }

        protected virtual void subSetValidCondition()
        {
            
        }

        protected virtual  void subSetContolRead()
        {
            
        }

        private   void subSaveFormLayOut()
        {
            common.SaveFormLayOut(this, this.layOUT, this.grpHead);
            common.SaveLayout(this.ListView);
        }

        private void subRestoreFormLayOut()
        {
            common.RestoreFormLayOut(this, this.layOUT, this.grpHead);
            common.ReStoreLayOut(this.ListView);
        }

        protected virtual void subSetDataSource()
        {
            
        }
        #endregion

        #region 窗体关闭

        private void frmBaseBasicEntry_FormClosing(object sender, FormClosingEventArgs e)
        {
            //subSaveFormLayOut();
        }
        #endregion

    }
}
